package com.lenovo.smartengine.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import com.dolby.api.DsGlobalEx;
import com.lenovo.smartengine.utils.AudioTypeSetUtils;
import com.lenovo.smartengine.utils.Constants;

public class IncomingAudioTypeReceiverEngine extends BroadcastReceiver{
	private final String TAG = "MainActivity";
    private DsGlobalEx mDsClient = null;
    
    public IncomingAudioTypeReceiverEngine(DsGlobalEx mDsClient){
    	this.mDsClient = mDsClient;
    }
    
	@Override
	public void onReceive(Context context, Intent intent) {
		Log.d(TAG, "onReceive...");
	    int type_value = 0;
		String media_type = null;
		String recorderPackageName = null;
		// receive the broadcasting message of media_type
		media_type = intent.getStringExtra(Constants.media_type);
		type_value = intent.getIntExtra(Constants.type_vaule,Constants.BASE_TYPE);
		recorderPackageName = intent.getStringExtra(Constants.recorder_name);
		
		Log.i(TAG,"media_type = "+media_type+", recordername= "+recorderPackageName);
		int phonemediatype = -1;
		
		int dolby_type_value = -1;
		//change the type_value to Dolby media type value
		 if(media_type!=null && type_value==1000){
			 dolby_type_value = 1001;
		 }else if(media_type!=null && type_value==1001){
			 dolby_type_value = 1000;
		 }else if(media_type!=null && type_value==1002){
			 dolby_type_value = type_value;
		 }
		
	    if (media_type!=null && mDsClient!=null && AudioTypeSetUtils.isDolbyApp(mDsClient)) {
			Log.i(TAG,"isDolbyApp...");
			phonemediatype = AudioTypeSetUtils.queryAudioTypeFromDolby(mDsClient);
			AudioTypeSetUtils.smartEngineToSwitchMediaTypeForDolby(context,mDsClient,
					phonemediatype, media_type, dolby_type_value);
			Log.i(TAG,"type_value = "+dolby_type_value+", phonemediatype ="+phonemediatype);
		}else if(media_type!=null && AudioTypeSetUtils.isWavesApp(context)){
			Log.i(TAG,"isWavesApp...");
			phonemediatype = AudioTypeSetUtils.queryAudioTypeFromWaves(context);
			AudioTypeSetUtils.smartEngineToSwitchMediaTypeForWaves(context, phonemediatype,
					media_type, type_value);
			Log.i(TAG,"type_value = "+type_value+", phonemediatype ="+phonemediatype);
		}

	}
	
	@Override
	public IBinder peekService(Context context, Intent service) {
		// TODO Auto-generated method stub
		Log.d(TAG, "peekService...");
		return super.peekService(context, service);
	}
}
